//将addmebber 和 departmentIndex中的添加成员中相同的配置抽取
export default {
	onLoad() { 
		let that = this;
		uni.getSystemInfo({
			success: function(res) {
				let pxToRpxScale = 750 / res.windowWidth;
				// console.log(res.windowHeight);
	
				that.height = res.windowHeight * pxToRpxScale;
				// console.log(that.height)
			}
		})
	},
	data() {
		return {
			noData: true,
			isName: true, 
			isAnother: true,
			lineHeight: true,
			result: [],
			currentChoose: [],
			choose: [], //用于存放本轮查询中，currentChoose已有的数组
			checkedChoose: [],  //用于存放每次选择的数组，一直变
			startIndex: 0,
			childrenId: '', //用于添加时，上面滚动条的移动
			num: 0, //用于是否时第一次查询，如果是的过，数据直接push到currentChoose就好
			params: {
				grade: "",
				collegeId: "",
				majorId: "",
				classId: "",
				currentPage: 1
			},
			nameParams: {
				content: '',
				currentPage: 1
			},
			timer: ''
		}
	},
	methods: {
		//切换查询
		cut(isAnother) {
			this.isAnother = isAnother;
			//返回页面顶部
			uni.pageScrollTo({
				scrollTop: 0, //距离页面顶部的距离
				duration: 300
		
			});
		},
		getSearchResult(isName,params) {
			this.search = true
			this.isName = isName;
			this.result = [];
			this.choose = [];
			this.num++;
			this.startIndex = this.currentChoose.length;
			
			this.params.collegeId = params.collegeId;
			this.params.majorId = params.majorId;
			this.params.classId = params.classId;
			this.params.grade = params.grade;
			this.params.currentPage = 1;
			this.searchResult()
		},
		//查询
		async searchResult() {
			uni.showLoading({
				title: '加载中..',
				mask: true
				
			});
			this.status = 'loading';
			let that = this;
			this.lineHeight = false				
			let res = await getApp().UniRequest("/mita/authority/student", "Get", this.params,
				this.header);
				// console.log(res)
				if (res.code === 20000) {
					// console.log(that.currentChoose,'当前已有')
					if (res.data.list.length != 0) {
						
						res.data.list.forEach((item, index) => {
							item = item.esobject.source;
							item.checked = false;
							// item.checked = false //添加一个属性用来判断当前值有无被选中
							for (let i = 0; i < that.currentChoose.length; i++) {
								if (item.no == that.currentChoose[i].esobject.source.no) {
									// console.log('执行')
									item.checked = true;
									that.choose.push(JSON.stringify(item));
									// console.log(that.choose,'当前选中push中')
									break;
								}
							}
						})
						// console.log(that.choose,'当前选中')
						this.result.push(...res.data.list);
						// console.log(this.result)
						// console.log(this.choose, '当前列表选中的');
						if (this.result.length != 0) {
							this.noData = false;
							//按班级排序
							let min = {};
							let index = 0;
							for (let i = 0; i < this.result.length - 1; i++) {
								for (let j = i; j < this.result.length; j++) {
									if (i === j) {
										min = this.result[i];
										index = i;
									} else {
										if (min.esobject.source.className.replace(/[^0-9]/ig, "") > this
											.result[j].esobject.source
											.className.replace(/[^0-9]/ig,
												"")) {
											index = j;
											min = this.result[j];
										}
									}
								}
								this.result[index] = this.result[i]
								this.result[i] = min;
							}
						}
						// console.log(this.result)
					}
				uni.hideLoading()
				}
				if (this.result.length == 0) {
					this.noData = true
				} else {
					this.noData = false;
				}
				// uni.hideLoading()
		},
		async getSearchInput(params,callback) {
			uni.showLoading({
				title: '加载中..',
				mask: true
				
			});
			this.search = true
			// if (params) {
			// 	console.log('执行')
			// 	this.status = 'loading';
			// } else {
			// 	this.status = 'loading';
			// }
			// console.log('执行')
			let that = this;
			let header = new Object()
			header.Authority = uni.getStorageSync("Authority");
			// console.log(this.result,'查询')
			let res = await getApp().UniRequest("/mita/authority/studentByName", "Get",this.nameParams,
				this.header);
				// console.log(res,111);
				if (res.code === 20000) {
								
					if (res.data.list.length != 0) {
						this.noData = false;
						
						res.data.list.forEach((item, index) => {
							item = item.esobject.source;
							item.checked = false;
							// item.checked = false //添加一个属性用来判断当前值有无被选中
							for (let i = 0; i < that.currentChoose.length; i++) {
								if (item.no == that.currentChoose[i].esobject.source.no) {
									// console.log('执行')
									item.checked = true;
									that.choose.push(JSON.stringify(item));
									// console.log('姓名查询,当前选中push中',that.choose)
									break;
								}
							}
						})
						if (callback) {
							this.result = res.data.list
						} else {
							this.result.push(...res.data.list);
						}
						
						
					}
					uni.hideLoading()
					// console.log(this.result)
				}
				if (this.result.length == 0) {
					this.noData = true
				} else {
					this.noData = false;
				}
				// uni.hideLoading()
		},
		
		//键盘输入事件
		async searchInput(nameParams,isName) {
			this.isName = isName;
			this.lineHeight = true
			this.result = [];
			// console.log('键盘11',this.result,nameParams.content)
			this.choose = [];
			this.num++;
			this.startIndex = this.currentChoose.length;
			// console.log(this.nameSearch);
			this.nameParams.content = nameParams.content;
			// console.log()
			this.nameParams.currentPage = 1;
			// console.log(new Date().getTime() - time);
			// let time = new Date().getTime();
			// console.log(time,'将调接口的时间');
			clearTimeout(this.timer); //注意把定时器清空，否则多个定时器会在任务栈中
			this.timer = setTimeout(() => {
				// console.log('调用接口');
				// console.log(this.nameParams.content);
				this.result = []
				this.getSearchInput(this.nameParams,1);
			},1000)
			
			
		
		},
		
		//选中
		checkboxChange(e) {
			let that = this;
			this.checkedChoose = [];
			this.checkedChoose = e.detail.value;
			let temp = []; //存放当前选择的所有成员的学号数组
			// console.log(that.currentChoose)
			that.currentChoose.forEach((item,index) => {
				temp.push(item.esobject.source.no)
			})
			if (this.currentChoose.length == 0) {
				this.num = 1;
				
			}
			// this.startIndex = this.currentChoose.length;
			if ((this.currentChoose.length == 0) || (this.num == 1)) {
				this.currentChoose = [];
				// console.log('执行',this.num )
				this.checkedChoose.forEach((item,index) => {
					this.currentChoose.push(JSON.parse(item));
					// console.log(JSON.parse(item))
				})
			} else {
				
				//若本轮查询中，已选中的在选择过程中，已经取消选择时，对已选中的数据进行取消
				//列表中原先有的，当本次选择中取消时，原先的也要删除
				if (this.choose.length == 0) {
					
				} else {
					let anotherTemp = [] //用于存放当前选中的学号数组
					 e.detail.value.forEach((item,index) => {
						 anotherTemp.push(JSON.parse(item).esobject.source.no)
					    
					 })
					this.choose.forEach((item, index) => {
						// console.log(this.choose,'当前选中')
						if (anotherTemp.indexOf(JSON.parse(item).no) == -1) {
							let index1 = temp.indexOf(JSON.parse(item).no);
							if (index1 != -1) {
								that.currentChoose.splice(index1, 1);
								
								that.choose.splice(index,1) //删除后，原先选中的也要删除，这样后续就和本轮选择一致
								that.startIndex--; //startIndex最开始就是为了表示此次新增的人员，起始值为数组的长度，所以如果当原先的值减少，则此次新增的就要往左移动
								// console.log(that.choose)
							}
						}
					})
				}
				
				
				//新增加的选择里，若下面选择删除的话，上面也要删除
				if (this.startIndex != 0) {
					// console.log(this.startIndex,'开始增加的坐标');
					// console.log(this.checkedChoose)
					
					for (let i = this.startIndex; i < this.currentChoose.length; i ++) {
						let isTrue = true;
						this.checkedChoose.forEach((item,index) => {
							// console.log(this.currentChoose[i].esobject.source.no);
							// console.log(JSON.parse(item).esobject.source.no)
							if (this.currentChoose[i].esobject.source.no == JSON.parse(item).esobject.source.no) {
								isTrue = false;
							}
						})
						if (isTrue) {  //表示currentchoose里有元素，当前选择中没有，要删除它
							this.currentChoose.splice(i,1);
						}
					}
				}
				
				//本轮选择中，已选的没有才push进去
				this.checkedChoose.forEach((item,index) => {
					if (temp.indexOf(JSON.parse(item).esobject.source.no) == -1) {
						
						that.currentChoose.push(JSON.parse(item));
					}
				})
			}
			this.setScroll();
		},
		
		// scroll-view 动态添加标签时，自动滚动到最左边,利用scroll-into-view属性
		setScroll() {
			let len = this.currentChoose.length;
			this.childrenId = 'id-' + (len - 1);
		}
	}
}